package com.shensi.adminCollect.service.station.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shensi.adminCollect.domain.station.UserVirtualGradeRel;
import com.shensi.adminCollect.enums.station.GradeExpireMode;
import com.shensi.adminCollect.mapper.station.UserVirtualGradeRelMapper;
import com.shensi.adminCollect.service.station.IUserVirtualGradeRelService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @date 2024/1/16
 */
@Service
public class UserVirtualGradeRelServiceImpl extends ServiceImpl<UserVirtualGradeRelMapper, UserVirtualGradeRel> implements IUserVirtualGradeRelService {
    @Override
    public UserVirtualGradeRel queryUserVirtualGrade(Long enterpriseId, Long userId, LocalDateTime time) {
        UserVirtualGradeRel form = new UserVirtualGradeRel();
        form.setUserId(userId);
        form.setEnterpriseId(enterpriseId);
        QueryWrapper<UserVirtualGradeRel> qw = Wrappers.query(form);
        qw.last("limit 1");
        UserVirtualGradeRel one = getOne(qw);
        if (one != null) {
            if (one.getExpireMode() == GradeExpireMode.FIXED_DAY) {
                if (one.getExpireTime().isBefore(time)) {
                    one = null;
                }
            }else if(one.getExpireMode() == GradeExpireMode.TIME_SLOT){
                LocalDate nowDate = time.toLocalDate();
                LocalTime nowTime = time.toLocalTime();
                if(nowDate.isBefore(one.getEffectStartDate()) || nowDate.isAfter(one.getEffectEndDate())
                        || nowTime.isBefore(one.getEffectStartTime()) || nowTime.isAfter(one.getEffectEndTime())){
                    one = null;
                }
            }
        }
        return one;
    }
}
